package com.atguigu.serviceedu.controller;


import com.atguigu.commonutils.R;
import com.atguigu.servicebase.exception.GuliException;
import com.atguigu.serviceedu.entity.EduTeacher;
import com.atguigu.serviceedu.entity.vo.TeacherQuery;
import com.atguigu.serviceedu.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author JY
 * @since 2021-01-10
 */
@Api(description="讲师管理")
@RestController
@RequestMapping("/serviceedu/teacher")
//@CrossOrigin
public class EduTeacherController {

    @Resource
    private EduTeacherService teacherService;

    //查询讲师所有数据
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("findAll")
    public R findAllTeacher(){
        //模拟异常
//        try {
//            int i = 10/0;
//        } catch (Exception e) {
//            throw new GuliException(20001,"出现自定义异常");
//        }

        List<EduTeacher> list = teacherService.list(null);
        return R.ok().data("items",list);
    }

    //逻辑删除讲师
    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("delete/{id}")
    public R removeById(@PathVariable String id){
        boolean flag = teacherService.removeById(id);
        return R.ok();
    }

    //讲师列表分页
    @ApiOperation(value = "分页讲师列表")
    @GetMapping("pageList/{page}/{limit}")
    public R pageList(@PathVariable Long page,@PathVariable Long limit){
        Page<EduTeacher> pageParam = new Page<>(page,limit);
        teacherService.page(pageParam,null);
        long total = pageParam.getTotal();
        List<EduTeacher> records = pageParam.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }

    //条件查询分页
    //可根据讲师名称name，讲师头衔level、讲师入驻时间gmt_create（时间段）查询
    @PostMapping("pageCondition/{page}/{limit}")
    public R pageCondition(@PathVariable Long page, @PathVariable Long limit,
                           @RequestBody(required = false) TeacherQuery teacherQuery){
        Page<EduTeacher> pageParam = new Page<>(page,limit);

        //构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //多条件组合查询
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //实现类似于Mybatis的动态sql
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)){
            wrapper.le("gmt_modified",end);
        }

        //按创建时间时间降序排序，最新的放前面
        wrapper.orderByDesc("gmt_create");

        teacherService.page(pageParam,wrapper);
        long total = pageParam.getTotal();
        List<EduTeacher> records = pageParam.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }


    //新增讲师
    @PostMapping("add")
    public R add(@RequestBody EduTeacher teacher){
        boolean save = teacherService.save(teacher);
        return save?R.ok():R.error();
    }

    //根据id查询讲师
    @GetMapping("getById/{id}")
    public R getById(@PathVariable String id){
        EduTeacher teacher = teacherService.getById(id);
        return R.ok().data("item",teacher);
    }

    //根据id修改讲师
    @PostMapping("update")
    public R updateById(@RequestBody EduTeacher teacher){
        boolean flag = teacherService.updateById(teacher);
        return flag?R.ok():R.error();
    }
}

